/**
 * 我的钱包
 * @author  Jello
 * @create  2018/10/15 16:21
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/
var token = "";
var openid = "";
var userName = "";
var userUrl = "";
var countentPhone = "";
var spinner = new Spinner(opts);

$(function() {
	openid = getParam("openid"); //微信openid
	userName = getParam("userName"); //用户昵称
	userUrl = getParam("userUrl"); //用户头像
	countentPhone = getParam("countentPhone"); //手机框 0弹出1不弹出
	var weixin_withdraw_sign = localStorage.getItem("weixin_withdraw_sign"); //判断不同的用户

	if(openid != "" && openid != "null" && openid != null) {
		sessionStorage.setItem("openid", openid);
	}
	if(userName != "" && userName != "null" && userName != null) {
		sessionStorage.setItem("weixin_userName", decodeURI(userName));
	}
	if(userUrl != "" && userUrl != "null" && userUrl != null) {
		sessionStorage.setItem("weixin_userUrl", userUrl);
	}

	if(countentPhone == "0") {
		$(".phonecover").show();
		$(".phonebox").show();
	} else {
		//获取个人资料
		var param = {};
		param.userid_sign = sessionStorage.getItem("openid");
		getUserMaterial(param, function(data) {
			var user_money = toDecimal(data.money);
			//余额
			$("#money").html(user_money);
			$(".balance-start").hide();
			$(".balance-money").show();
		});
		$("#user_url").attr("src", sessionStorage.getItem("weixin_userUrl"));
		$("#nickname").html(sessionStorage.getItem("weixin_userName"));
	}

	//获取验证码
	$("#code_btn").click(function() {
		var phoneNum = $("#phoneNum").val(); //手机号
		var flag = checkPhone(phoneNum);
		if(flag) {
			var param = {};
			param.phone = phoneNum;
			param.type = 6;
			//获取验证码接口上送
			gainCode(param, function(data) {
				token = data;
				//开启验证码定时器
				for(i = 1; i <= 60; i++) {
					if(i == 1) {
						window.setTimeout("countDown(" + i + ")");
					} else {
						window.setTimeout("countDown(" + i + ")", (i - 1) * 1000);
					}
				}
			});
		}
	})

	//手机号绑定确定
	$("#phone_btn").click(function() {
		var phone = $("#phoneNum").val(); //手机号
		var code = $("#code").val();
		//校验验证码接口上送
		var param = {};
		param.phone = phone;
		param.type = 6;
		param.code = code;
		param.token = token;
		mateCode(param, function() {
			var param = {};
			param.userid_sign = sessionStorage.getItem("openid");
			param.phone = phone;
			param.userName = sessionStorage.getItem("weixin_userName");
			param.userUrl = sessionStorage.getItem("weixin_userUrl");
			//微信绑定客户手机号
			bindCustomerPhone(param, function(resultObj) {
				spinner.spin();
				//获取个人资料
				var param = {};
				param.userid_sign = sessionStorage.getItem("openid");
				getUserMaterial(param, function(data) {
					var user_money = toDecimal(data.money);
					//余额
					$("#money").html(user_money);
					$(".balance-start").hide();
					$(".balance-money").show();
				});
				$("#nickname").html(sessionStorage.getItem("weixin_userName"));
				$("#user_url").attr("src", sessionStorage.getItem("weixin_userUrl"));
				$(".phonecover").hide();
				$(".phonebox").hide();
			});
		})
	})

	//提现
	$("#withdraw_btn").click(function() {
		if(sessionStorage.getItem("openid")) {
			sessionStorage.setItem("wallet_sign", 001166);
			localStorage.setItem("weixin_withdraw_sign", sessionStorage.getItem("openid")); //验证不同用户登录后提现密码次数的问题
			if(sessionStorage.getItem("openid") != weixin_withdraw_sign) { //不是当前用户清除十分钟次数和cookie的时间
				localStorage.removeItem("sess_time_local");
				$.cookie("sess_time_local", "", {
					expires: -1,
					path: '/bluehired'
				})
			}
			//查询是否有银行卡，没有跳转银行卡页面，有直接到提现页面
			$.ajax({
				type: "get",
				url: serverUrl + "/billrecord/query_bankcardwithDraw?sgin=" + sessionStorage.getItem("openid"),
				cache: false,
				async: false,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				timeout: 15000,
				crossDomain: true,
				success: function(data) {
					var resultObj = eval(data);
					if(resultObj.code == 0 && resultObj.data != null) {
						if(resultObj.data.type == 1) { //没有绑定银行卡
							window.location.href = "app/bank/addcard.html";
						} else {
							sessionStorage.setItem("with_bankname", resultObj.data.bankName);
							sessionStorage.setItem("with_banknumber", resultObj.data.bankNumber);
							sessionStorage.setItem("with_cardtype", resultObj.data.cardType);
							sessionStorage.setItem("reg_phone", resultObj.data.phone);
							window.location.href = "app/withdraw/withdraw.html";
						}
					} else {
						mui.toast(resultObj.msg);
						return;
					}
				},
				error: function(XMLHttpRequest, status, errorThrown) {
					if(status == 'timeout') {
						mui.toast("请求超时");
					} else {
						mui.toast("请求网络服务数据失败");
					}
					return;
				}
			});
		}
	})

	//个人账单
	$("#bill_btn").click(function() {
		if(sessionStorage.getItem("openid")) {
			sessionStorage.setItem("wallet_sign", 001166);
			window.location.href = "app/bill/personalbill.html";
		}
	})

})